<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Main Page</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<link href="css/TestMain.css" rel="stylesheet" type="text/css" />
<link href="css/print.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/OperateTool.js"></script>
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script src="js/search.js" type="text/javascript"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
	droplinemenu.buildmenu("nav");
</script>


<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


<script src="js/search.js"></script>


<script>
	$(document).ready(function(){
		$("#calendar").datepicker({
			showButtonPanel:false,
			inline:false,
			onSelect:timeChange,
			dateFormat:'yy/mm/dd',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});

		var datepicker_CurrentInput;  
        $.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        $(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		$("#birth").datepicker({
			showButtonPanel:true,
			dateFormat:'dd-mm-yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});

	});


	function timeChange(dateText){
		document.getElementById("specifiedDate").value = dateText;

		jQuery.ajax({type:'post', async:false,dataType:'json', url:'get_appointment_by_date.php', data:"date=" + dateText, success:function(result){
				var divElement = document.getElementById("normalAppointmentList");
				
				var childs = divElement.childNodes;
				
				
				for( var i = childs.length - 1; i >= 0; i-- ){
					divElement.removeChild(childs[i]);
				}
				
				var tableObject = document.createElement("table");
				tableObject.setAttribute("width","660px");
				tableObject.innerHTML = "<tr><th>No.</th><th>Patient</th><th>Mobile</th><th>DOB</th><th>Date</th><th>Time</th><th>Visit</th></tr>";
				for( var i = 0; i < result.data.length; i++ ){

					var appointmentNoTd = document.createElement("td");
					var appointmentLink = document.createElement("a");
					appointmentLink.href = "appointment.php?appointment_no=" + result.data[i].appointmentNo;
					appointmentLink.innerHTML = result.data[i].appointmentNo;
					appointmentNoTd.appendChild(appointmentLink);
					appointmentNoTd.setAttribute("width", "10%");
					
					
					var patientNameTd = document.createElement("td");
					var patientNoLink = document.createElement("a");
					patientNoLink.href = "patient_account.php?patientNo=" + result.data[i].patientNo;
					patientNoLink.innerHTML = result.data[i].patientName;
					patientNameTd.appendChild(patientNoLink);
					patientNameTd.setAttribute("width", "25%");

					
					//var doctorNameTd = document.createElement("td");
					//doctorNameTd.innerHTML = result.data[i].doctorName;
					//doctorNameTd.width = "25%";

					var mobileTd = document.createElement("td");
					mobileTd.innerHTML = result.data[i].mobile;
					mobileTd.width = "12%";

					var dateOfBirthTd = document.createElement("td");
					dateOfBirthTd.innerHTML = result.data[i].dateOfBirth;
					dateOfBirthTd.width = "20%";
					
					
					var dateTd = document.createElement("td");
					dateTd.innerHTML = result.data[i].appointmentDate;
					dateTd.width = "20%";
					
					var timeTd = document.createElement("td");
					timeTd.innerHTML = result.data[i].appointmentTime;
					timeTd.width = "10%";
					var viewTd = document.createElement("td");
					viewTd.width = "10%";

					if( result.data[i].type == 3 ){
						
						var viewElement = document.createElement("a");
						viewElement.href="review_patient.php?appointmentNo=" + result.data[i].appointmentNo;
						
						viewElement.innerHTML = "view";
						viewTd.appendChild(viewElement);	
					}
					
					var childTrElement = document.createElement("tr");
					childTrElement.id = result.data[i].appointmentNo;
					

					childTrElement.appendChild(appointmentNoTd);
					childTrElement.appendChild(patientNameTd);
					childTrElement.appendChild(mobileTd);
					childTrElement.appendChild(dateOfBirthTd);
					childTrElement.appendChild(dateTd);
					childTrElement.appendChild(timeTd);
					childTrElement.appendChild(viewTd);
					

					tableObject.appendChild(childTrElement);
				}
				divElement.appendChild(tableObject);
			},
			error:function(XMLHttpRequest, textStatus, errorThrown ){
				alert(XMLHttpRequest.responseText);
			}
		});
	}

	function typeChange(type){
		var postData = "type=" + type;
		jQuery.ajax({type:'post', async:false,dataType:'json', url:'get_unsigned_appointment.php', data:postData, success:function(result){
				var tableObject = document.getElementById("unsignedTableBody");
				var trList = jQuery("#unsignedTableBody>tr:has(td)");

				for( var i = 0; i < trList.length; i++ ){
					tableObject.removeChild(trList[i]);
				}
				
				
				for( var i = 0; i < result.data.length; i++ ){

					var trElement = document.createElement("tr");

					var appointmentNoTd = document.createElement("td"); 
					appointmentNoTd.innerHTML = result.data[i].appointmentNo;
					trElement.appendChild(appointmentNoTd);

					var patientNameTd = document.createElement("td"); 
					patientNameTd.innerHTML = result.data[i].patientName;
					trElement.appendChild(patientNameTd);


					
					var doctorNameTd = document.createElement("td");
					doctorNameTd.innerHTML = result.data[i].doctorName;
					trElement.appendChild(doctorNameTd);



					
					var typeTd = document.createElement("td");
					if( result.data[i].sub_type == "1" ){
						typeTd.innerHTML = "typeOne";
					}else if( result.data[i].sub_type == "2" ){
						typeTd.innerHTML = "typeTwo";
					}else if( result.data[i].sub_type == "3" ){
						typeTd.innerHTML = "typeThree";
					}else{
						typeTd.innerHTML = "unknown";
					}
					trElement.appendChild(typeTd);

					var operateTd = document.createElement("td");
					var signedElement = document.createElement("a");
					signedElement.href = "appointment.php?appointment_no=" + result.data[i].appointmentNo;
					signedElement.innerHTML = "signed";
					operateTd.appendChild(signedElement);
					trElement.appendChild(operateTd);
					

					tableObject.appendChild(trElement);

				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
	}

	function printAppointment(){
		var dateText = document.getElementById("specifiedDate").value;
		jQuery.ajax({type:'post', async:false,dataType:'json', url:'get_appointment_by_date.php', data:"date=" + dateText, success:function(result){
				var tableObject = document.getElementById("printTable");

				var trList = jQuery("#printTable>tr:has(td)");

				for( var i = 0; i < trList.length; i++ ){
					tableObject.removeChild(trList[i]);
				}
				document.getElementById("currentDateLabel").innerHTML = result.date;
				
				for( var i = 0; i < result.data.length; i++ ){

					var trElement = document.createElement("tr");

					var appointmentTimeTd = document.createElement("td"); 
					appointmentTimeTd.innerHTML = result.data[i].appointmentTime;
					trElement.appendChild(appointmentTimeTd);

					var patientNameTd = document.createElement("td"); 
					patientNameTd.innerHTML = result.data[i].patientName;
					trElement.appendChild(patientNameTd);


					
					var patientNoTd = document.createElement("td");
					if( result.data[i].isFirstVisit == 0 ){
						patientNoTd.innerHTML = result.data[i].patientNo;
					}else{
						patientNoTd.innerHTML = "New";
					}
					trElement.appendChild(patientNoTd);

					

					
					var dateLastAKTd = document.createElement("td"); 
					dateLastAKTd.innerHTML = result.data[i].dateLastAK;
					trElement.appendChild(dateLastAKTd);

					var dateLastSLTd = document.createElement("td"); 
					dateLastSLTd.innerHTML = result.data[i].dateLastSL;
					trElement.appendChild(dateLastSLTd);

					var nextReviewTd = document.createElement("td"); 
					nextReviewTd.innerHTML = "";
					trElement.appendChild(nextReviewTd);


					
					var landlineTd = document.createElement("td"); 
					landlineTd.innerHTML = result.data[i].landline;
					trElement.appendChild(landlineTd);

					var mobileTd = document.createElement("td"); 
					mobileTd.innerHTML = result.data[i].mobile;
					trElement.appendChild(mobileTd);

					//trElement.appendChild(document.createElement("td"));
					trElement.appendChild(document.createElement("td"));
					trElement.appendChild(document.createElement("td"));

					tableObject.appendChild(trElement);

				}

				document.getElementById("printDiv").style.display = "block";
				document.getElementById("container").style.display = "none";
				window.print();
				document.getElementById("printDiv").style.display = "none";
				document.getElementById("container").style.display = "block";
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
	}

	function printUnsignAppointment(){
		var type = document.getElementById("unsignedType").value;
		var postData = "type=" + type;
		jQuery.ajax({type:'post', async:false,dataType:'json', url:'get_unsigned_appointment.php', data:postData, success:function(result){
				var tableObject = document.getElementById("printUnsignTable");

				var trList = jQuery("#printUnsignTable>tr:has(td)");

				for( var i = 0; i < trList.length; i++ ){
					tableObject.removeChild(trList[i]);
				}
				document.getElementById("headLabel").innerHTML = "Unsigned Appointment";
				
				for( var i = 0; i < result.data.length; i++ ){

					var trElement = document.createElement("tr");

					//var appointmentTimeTd = document.createElement("td"); 
					//appointmentTimeTd.innerHTML = result.data[i].appointmentTime;
					//trElement.appendChild(appointmentTimeTd);

					var patientNameTd = document.createElement("td"); 
					patientNameTd.innerHTML = result.data[i].patientName;
					trElement.appendChild(patientNameTd);


					
					var patientNoTd = document.createElement("td");
					if( result.data[i].isFirstVisit == 0 ){
						patientNoTd.innerHTML = result.data[i].patientNo;
					}else{
						patientNoTd.innerHTML = "New";
					}
					trElement.appendChild(patientNoTd);



					
					var dateLastAKTd = document.createElement("td"); 
					dateLastAKTd.innerHTML = result.data[i].dateLastAK;
					trElement.appendChild(dateLastAKTd);

					var dateLastSLTd = document.createElement("td"); 
					dateLastSLTd.innerHTML = result.data[i].dateLastSL;
					trElement.appendChild(dateLastSLTd);

					var nextReviewTd = document.createElement("td"); 
					nextReviewTd.innerHTML = "";
					trElement.appendChild(nextReviewTd);


					
					var landlineTd = document.createElement("td"); 
					landlineTd.innerHTML = result.data[i].landline;
					trElement.appendChild(landlineTd);


					tableObject.appendChild(trElement);

				}

				document.getElementById("printUnsignDiv").style.display = "block";
				document.getElementById("container").style.display = "none";
				window.print();
				document.getElementById("printUnsignDiv").style.display = "none";
				document.getElementById("container").style.display = "block";
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
				alert(textStatus);
				alert(errorThrown);
			}
		});
	}
        
</script>

</head>

<body>
	<div id="container">
	<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php include_once 'search_patient.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
		
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			date_default_timezone_set($ini_array["default_timezone"]);
			$date = date("Y-m-d");
			$currentDay = date("d/m/Y H:i:s", strtotime($date));
			$nextDay = date("d/m/Y H:i:s",strtotime($date) + 60*60*24);
			
			$getAppointmentSql = "select appointment_no, First_name, Surname, appointment_datetime,appointment.type, patient.Patient_no, mobile, Date_birth from appointment, patient where appointment.Patient_no=patient.Patient_no and  appointment.type != 1 and appointment_datetime < str_to_date('".$nextDay."','%d/%m/%Y %H:%i:%s') and appointment_datetime > str_to_date('".$currentDay."','%d/%m/%Y %H:%i:%s') order by appointment_datetime desc";
			//echo $getAppointmentSql;
			
			$appointmentResult = mysqli_query($conn, $getAppointmentSql);
		?>
		<div id="main_content">
			<div id="top_content">
			<div class="transbg">
				<div id="normalAppointment">
					<h3>Appointment List</h3>

					
    					<div id="normalAppointmentList">
    						<table style="width:660px">
    							<thead>
	    						    <tr>
	    								<th style="width:8%;">No.</th>
	    								<th style="width:29%;">Patient</th>
	    								<th style="width:12%;">Mobile</th>
	    								<th style="width:20%;">DOB</th>
	    								<th style="width:22%;">Date</th>
	    								<th style="width:8%;">Time</th>
	    								<th style="width:8%;">Visit</th>
	    							</tr>
    							</thead>
    							<?php 
    								$appointmentCount = mysqli_num_rows($appointmentResult);
    								while( $row= mysqli_fetch_array($appointmentResult)){
    									$appointmentNo = $row["appointment_no"];
    									$patientNo = $row["Patient_no"];
    									$patientFirstName = $row["First_name"];
    									if( strlen($patientFirstName) > 15 ){
											$patientFirstName = substr($patientFirstName, 0, 15);
										}
										$patientSurname = $row["Surname"];
										if( strlen($patientSurname) > 15 ){
											$patientSurname = substr($patientSurname, 0, 15);
										}
										$mobile = $row["mobile"];
										$dateOfBirth = $row["Date_birth"];
										
    									$patientName = $patientFirstName."  ".$patientSurname;
    									$doctorName = "Geraldine";
    									$appointmentDate = date("d-m-Y",strtotime($row["appointment_datetime"]));
    									$appointmentTime = date("H:i",strtotime($row["appointment_datetime"]));
    									$type = $row["type"];
    		
    							?>
    							<tbody>
	    							<tr>
	    								<td style="width:8%;"><a href="appointment.php?appointment_no=<?php echo $appointmentNo?>"><?php echo $appointmentNo?></a></td>
	    								<td style="width:29%;"><a href="patient_account.php?patientNo=<?php echo $patientNo ?>"><?php echo $patientName?></a></td>
	    								<!-- <td style="width:25%;"><?php echo $doctorName?></td>-->
	    								<td style="width:12%;"><?php echo $mobile?></td>
	    								<td style="width:20%;"><?php echo $dateOfBirth?></td>
	    								<td style="width:22%;"><?php echo $appointmentDate?></td>
	    								<td style="width:8%;"><?php echo $appointmentTime?></td>
	    								<td style="width:8%;"><?php if( $type == 3 ){echo "<a href='review_patient.php?appointmentNo=".$appointmentNo."'>view</a>";}?></td>
	    							</tr>
    							</tbody>
    							<?php }?>
    						</table>
    					</div>
					
					<input type="button" value="print" onclick="printAppointment()" />
					<input type="hidden" id="specifiedDate" value="<?php echo date("Y/m/d")?>" />
				</div>
			</div>
				<div id="calendar">
				</div>
			</div>
			<div class="clearFloat"></div>
			<div id="bottom_content">
				<?php 
					$getUnsignSql = "select appointment_no, First_name, Surname, appointment_datetime,sub_type  from appointment, patient where appointment.Patient_no=patient.Patient_no and appointment.type=1";
					$unsignResult = mysqli_query($conn, $getUnsignSql);
					
				?>
				<div id="unsignedAppointment" >
					<h3>Unassigned Datetime Appointment List</h3>		
					<div id="unsignedAppointList" style="margin:10px auto;width:600px">
						<table style="width:580px" id="unsignedTable">
							<thead>
								<tr>
									<th style="width:15%;">NO.</th>
									<th style="width:28%;">Patient</th>
									<th style="width:15%;">Doctor</th>
									<th style="width:15%;">Type</th>
									<th style="width:25%;">Operate</th>
								</tr>
							</thead>
							<tbody id="unsignedTableBody">
								<?php 
									while( $row= mysqli_fetch_array($unsignResult)){
				
										$appointmentNo = $row["appointment_no"];
										$patientFirstName = $row["First_name"];
										if( strlen($patientFirstName) > 15 ){
											$patientFirstName = substr($patientFirstName, 0, 15);
										}
										$patientSurname = $row["Surname"];
										if( strlen($patientSurname) > 15 ){	
											$patientSurname = substr($patientSurname, 0, 15);
										}
										$patientName = $patientFirstName."  ".$patientSurname;
										$doctorName = "Geraldine";
										$unsignedType = $row["sub_type"];
										$unsignedTypeStr = "";
										if( $unsignedType == 1 ){
											$unsignedTypeStr = "typeOne";
										}else if( $unsignedType == 2 ){
											$unsignedTypeStr = "typeTwo";
										}else if( $unsignedType == 3 ){
											$unsignedTypeStr = "typeThree";
										}else{
											$unsignedTypeStr = "unknown";
										}
								?>
								<tr>
									<td width="15%"><?php echo $appointmentNo?></td>
									<td width="28%"><?php echo $patientName?></td>
									<td width="15%"><?php echo $doctorName?></td>
									<td width="15%"><?php echo $unsignedTypeStr?></td>
									<td width="25%"><a href="appointment.php?appointment_no=<?php echo $appointmentNo?>">Assign Date</a></td>
								</tr>
								<?php }?>
							</tbody>
						</table>
					</div>
					<select id="unsignedType" onchange="typeChange(this.value)">
						<option value="0">all type</option>
						<option value="1">typeOne</option>
						<option value="2">typeTwo</option>
						<option value="3">typeThree</option>
					</select>
					<input type="button" value="print" onclick="printUnsignAppointment()" />
				</div>
				<?php 
					$getUnpaidCountSql = "select count(*) count from visit where ispay=false";
					$unpaidCountResult = mysqli_query($conn, $getUnpaidCountSql);
					$unpaidRow = mysqli_fetch_array($unpaidCountResult);
					
					
					$getUnprintedSql = "select count(*) count from visit where is_printed=0";
					$getUnprintedResult = mysqli_query($conn, $getUnprintedSql);
					$unprintedRow = mysqli_fetch_array($getUnprintedResult);
				?>
				
				<div id="status" style="text-align:center">
					<h3>Status</h3>
					<div style="margin:10px auto;width:250px;text-align:center;">
						<table style="width:250px">
							<tr><td style="width:30%;text-align:center"><a onclick='timeChange("<?php echo date('Y-m-d')?>")' href="#"><?php echo $appointmentCount?></a></td><td style="width:70%"><label>appointments today</label></td></tr>
							<tr><td style="width:30%;text-align:center"><a href="list_unpaid.php"><?php echo $unpaidRow["count"]?></a></td><td><label>Unpaid Bill</label></td></tr>
							<tr><td style="width:30%;text-align:center"><a href="list_unprint_report.php"><?php echo $unprintedRow["count"]?></a></td><td><label>unprinted letters</label></td></tr>
						</table>
					</div>
				</div>
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
	<div id="printDiv" style="display:none;">
		<label id="currentDateLabel"></label>
		<div style="margin:0px auto;width:700px;">
			<table id="printTable" style="width:100%;">
				<tr >
					<th width='8%'>Time</th>
					<th width='14%'>Patient Name</th>
					<th width='14%'>Pt No</th>
					<th width='8%'>Last AK</th>
					<th width='8%'>Last SL</th>
					<th width='8%'>Next Review</th>
					<th width='15%'>Landline</th>
					<th width='15%'>Mobile</th>
					<th width='5%'>BK 2</th>
					<th width='5%'>BK 3</th>
				</tr>
			</table>
		</div>
	</div>
	<div id="printUnsignDiv" style="display:none;">
		<label id="headLabel"></label>
		<div style="width:700px;margin:0px auto;">
			<table id="printUnsignTable" style="width:100%;">
				<tr >
					<th width='30%'>Patient Name</th>
					<th width='20%'>Pt No</th>
					<th width='10%'>Last AK</th>
					<th width='10%'>Last SL</th>
					<th width='10%'>Next Review</th>
					<th width='10%'>Phone</th>
				</tr>
			</table>
		</div>
	</div>

</body>
</html>
